package com.example.teatea.dao;

import com.example.teatea.project.Good;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class GetGood {
    public List<Good> getGood(int i){
        List<Good> list = new ArrayList<>();
        Connection connection = null;
        ResultSet resultSet = null;
        Properties info = new Properties();
        try {
            info.load(new FileInputStream("src\\main\\resources\\com\\example\\teatea\\sql.properties"));
            String user=info.getProperty("user");
            String password=info.getProperty("password");
            String Driver=info.getProperty("Driver");
            String url=info.getProperty("url");
            //注册驱动
            Class.forName(Driver);
            //获取连接
            connection = DriverManager.getConnection(url, user, password);
            String sql = "select * from goods where gType = ?";
            PreparedStatement presta = connection.prepareStatement(sql);
            presta.setInt(1, i);
            resultSet = presta.executeQuery();
            while (resultSet.next()) {
                Good good = new Good();
                good.setgName(resultSet.getString("gName"));
                good.setgPrice(resultSet.getFloat("gPrice"));
                good.setgNumber(resultSet.getInt("gNumber"));
                String s = new String();
                int type = resultSet.getInt("gType");
                switch (type){
                    case 1: s = "茶";break;
                    case 2: s = "咖啡";break;
                    case 3: s = "便餐";break;
                    case 4: s = "娱乐项目";break;
                }
                good.setgType(s);
                good.setgImage(resultSet.getString("gPhoto"));
//                System.out.println(good.getgName());
                list.add(good);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }
}
